package com.studentmanager;

import com.wdd.studentmanager.domain.Clazz;
import com.wdd.studentmanager.mapper.ClazzMapper;
import com.wdd.studentmanager.service.Impl.ClazzServiceImpl;
import com.wdd.studentmanager.util.PageBean;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static org.junit.Assert.assertEquals;
import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.Mockito.*;

public class classtest {


        @InjectMocks
        private ClazzServiceImpl clazzService; // The service under test

        @Mock
        private ClazzMapper clazzMapper; // Mocked dependency

        @BeforeEach
        void setUp() {
            MockitoAnnotations.openMocks(this); // Initialize mocks
        }

        @Test
        void testQueryPage() {
            // Arrange
            Map<String, Object> paramMap = new HashMap<>();
            paramMap.put("pageno", 1);
            paramMap.put("pagesize", 10);

            List<Clazz> mockData = Arrays.asList(new Clazz(), new Clazz());
            when(clazzMapper.queryList(paramMap)).thenReturn(mockData);
            when(clazzMapper.queryCount(paramMap)).thenReturn(20);

            // Act
            PageBean<Clazz> result = clazzService.queryPage(paramMap);

            // Assert
            assertNotNull(result);
            assertEquals(2, result.getDatas().size());
            assertEquals(20, result.getTotalsize());
            verify(clazzMapper, times(1)).queryList(paramMap);
            verify(clazzMapper, times(1)).queryCount(paramMap);
        }

        @Test
        void testAddClazz() {
            // Arrange
            Clazz clazz = new Clazz();
            when(clazzMapper.addClazz(clazz)).thenReturn(1);

            // Act
            int result = clazzService.addClazz(clazz);

            // Assert
            assertEquals(1, result);
            verify(clazzMapper, times(1)).addClazz(clazz);
        }

        @Test
        void testDeleteClazz() {
            // Arrange
            List<Integer> ids = Arrays.asList(1, 2, 3);
            when(clazzMapper.deleteClazz(ids)).thenReturn(3);

            // Act
            int result = clazzService.deleteClazz(ids);

            // Assert
            assertEquals(3, result);
            verify(clazzMapper, times(1)).deleteClazz(ids);
        }

        @Test
        void testEditClazz() {
            // Arrange
            Clazz clazz = new Clazz();
            when(clazzMapper.editClazz(clazz)).thenReturn(1);

            // Act
            int result = clazzService.editClazz(clazz);

            // Assert
            assertEquals(1, result);
            verify(clazzMapper, times(1)).editClazz(clazz);
        }

        @Test
        void testFindByName() {
            // Arrange
            String clazzName = "Class A";
            Clazz mockClazz = new Clazz();
            when(clazzMapper.findByName(clazzName)).thenReturn(mockClazz);

            // Act
            Clazz result = clazzService.findByName(clazzName);

            // Assert
            assertNotNull(result);
            verify(clazzMapper, times(1)).findByName(clazzName);
        }

        @Test
        void testGetAllByCid() {
            // Arrange
            int clazzId = 1;
            List<Clazz> mockData = Arrays.asList(new Clazz(), new Clazz());
            when(clazzMapper.getAllBycid(clazzId)).thenReturn(mockData);

            // Act
            List<Clazz> result = clazzService.getAllBycid(clazzId);

            // Assert
            assertNotNull(result);
            assertEquals(2, result.size());
            verify(clazzMapper, times(1)).getAllBycid(clazzId);
        }
    }

